package com.uduemc.biso.web.api.server.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.uduemc.biso.core.extities.center.SysServer;
import com.uduemc.biso.core.feign.SysServerFeign;
import com.uduemc.biso.core.property.GlobalProperties;
import com.uduemc.biso.core.utils.RedisUtil;
import com.uduemc.biso.core.utils.RestResult;
import com.uduemc.biso.core.utils.ResultUtil;
import com.uduemc.biso.web.api.server.ServerService;

@Service
public class ServerServiceImpl implements ServerService {

	private static final Logger logger = LoggerFactory.getLogger(ServerServiceImpl.class);

	@Autowired
	private RedisUtil redisUtil;

	@Autowired
	private GlobalProperties globalProperties;

	@Autowired
	private SysServerFeign sysServerFeign;

	@Autowired
	private ObjectMapper objectMapper;

	@SuppressWarnings("unchecked")
	@Override
	public List<SysServer> findOkAll() {
		String KEY = globalProperties.getCenterRedisKey().getSysServerFindOkAllKey();
		List<SysServer> sysServerOkAll = (List<SysServer>) redisUtil.get(KEY);
		if (sysServerOkAll == null) {
			logger.info("获取 sysServerOkAll 数据存入缓存中");
			RestResult restResult = sysServerFeign.findByTypeStatus(0, (short) 1);
			if (restResult.getCode() != 200 || restResult.getData() == null) {
				logger.error("serverFeign.findOkAll(); restResult.getCode() != 200 || restResult.getData() == null");
			} else {
				JavaType javaType = objectMapper.getTypeFactory().constructParametricType(ArrayList.class,
						SysServer.class);
				try {
					sysServerOkAll = (List<SysServer>) objectMapper
							.readValue(objectMapper.writeValueAsString(restResult.getData()), javaType);
				} catch (Exception e) {
					e.printStackTrace();
				}
				if (!CollectionUtils.isEmpty(sysServerOkAll)) {
					redisUtil.set(KEY, sysServerOkAll, 3600 * 24 * 10);
				}
			}
		}

		return sysServerOkAll;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SysServer> getAllowAccessIPServer() {
		String KEY = globalProperties.getCenterRedisKey().getSysServerAllowAccessIPServer();
		List<SysServer> sysServerAllow = (List<SysServer>) redisUtil.get(KEY);
		if (sysServerAllow == null) {
			RestResult restResult = null;
			List<SysServer> data01 = null;
			List<SysServer> data03 = null;
			List<SysServer> data11 = null;
			try {
				restResult = sysServerFeign.findByTypeStatus(0, (short) 1);
				data01 = (ArrayList<SysServer>) ResultUtil.data(restResult, new TypeReference<ArrayList<SysServer>>() {
				});

				restResult = sysServerFeign.findByTypeStatus(0, (short) 3);
				data03 = (ArrayList<SysServer>) ResultUtil.data(restResult, new TypeReference<ArrayList<SysServer>>() {
				});

				restResult = sysServerFeign.findByTypeStatus(1, (short) 1);
				data11 = (ArrayList<SysServer>) ResultUtil.data(restResult, new TypeReference<ArrayList<SysServer>>() {
				});
			} catch (IOException e) {
				e.printStackTrace();
			}

			sysServerAllow = new ArrayList<>();

			if (!CollectionUtils.isEmpty(data01)) {
				sysServerAllow.removeAll(data01);
				sysServerAllow.addAll(data01);
			}

			if (!CollectionUtils.isEmpty(data03)) {
				sysServerAllow.removeAll(data03);
				sysServerAllow.addAll(data03);
			}

			if (!CollectionUtils.isEmpty(data11)) {
				sysServerAllow.removeAll(data11);
				sysServerAllow.addAll(data11);
			}

			if (!CollectionUtils.isEmpty(sysServerAllow)) {
				redisUtil.set(KEY, sysServerAllow, 3600 * 24 * 10);
			}

		}

		return sysServerAllow;
	}

}
